import requests
import json
headers = {
    'Authorization': 'Basic YWRtaW46YWRtaW4='
}


def openfaas_login():
    login_url = "http://192.168.205.128:31112/ui/"
    try:
        response = requests.get(login_url, headers=headers)
        response.raise_for_status()
        return response.cookies
    except:
        return None


def get_openfaas_functions_info():
    function_info_url = 'http://192.168.205.128:31112/system/functions'
    try:
        response = requests.get(function_info_url, headers=headers, cookies=openfaas_login())
        response.raise_for_status()
        bytes = response.content
        function_info_str = bytes.decode('UTF-8')
        function_info_dir = json.loads(function_info_str)
        # print(function_info_dir[0]['name'])
        return function_info_dir
    except:
        return None

def check_function_name_is_exits(request):
    function_infos = get_openfaas_functions_info()
    function_name_list = []
    for function_info in function_infos:
        function_name_list.append(function_info['name'])
    function_name = request.GET['fname']
    if function_name in function_name_list:
        return True
    else:
        return False